372B - Counting Rectangles is Fun - CodeForces Solution


brute force divide and conquer dp *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

#define dbg(x) cerr << #x << " = " << (x) << "\n"
#define bit(n,k) (((n)>>(k))&1)
#define rep(i,a,b) for(long long i = (a); i <= (b); ++i)
#define per(i,a,b) for(long long i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef pair<int, int> i2;
typedef pair<ll, ll> ll2;
typedef long double ld;

const int N = 49;
int rect[N][N][N][N];
int dp[N][N][N][N];

void testcase() {
	
	int n, m, q; cin >> n >> m >> q;
	rep(i, 1, n) {
		rep(j, 1, m) {
			char c; cin >> c;
			if(c == '1') rect[i][j][i][j] = 1;
		}
	}
	rep(h, 1, n) {
		rep(w, 1, m) {
			rep(i, 1, n-h+1) {
				rep(j, 1, m-w+1) {
					rect[i][j][i+h-1][j+w-1] = rect[i][j][i+h-1][j+w-2]+rect[i][j][i+h-2][j+w-1]-rect[i][j][i+h-2][j+w-2]+rect[i+h-1][j+w-1][i+h-1][j+w-1];
				}
			}
		}
	}
	rep(h, 1, n) {
		rep(w, 1, m) {
			rep(i, 1, n-h+1) {
				rep(j, 1, m-w+1) {
					dp[i][j][i+h-1][j+w-1] = (rect[i][j][i+h-1][j+w-1] == 0 ? 1 : 0);
					
					dp[i][j][i+h-1][j+w-1] += dp[i][j][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i][j+1][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i][j][i+h-2][j+w-1];
					
					dp[i][j][i+h-1][j+w-1] -= dp[i][j][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i][j+1][i+h-2][j+w-1];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j+1][i+h-1][j+w-1];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i][j+1][i+h-1][j+w-2];
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j][i+h-2][j+w-1];
					
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i][j+1][i+h-2][j+w-2];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j+1][i+h-2][j+w-1];
					dp[i][j][i+h-1][j+w-1] += dp[i+1][j+1][i+h-1][j+w-2];
					
					dp[i][j][i+h-1][j+w-1] -= dp[i+1][j+1][i+h-2][j+w-2];
				}
			}
		}
	}
	while(q--) {
		int a, b, c, d; cin >> a >> b >> c >> d;
		cout << dp[a][b][c][d] << "\n";
	}
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int t = 1;
	//cin >> t;
	while(t--) testcase();

	return 0;
}


Comments

Submit
0 Comments
More Questions

87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation